﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Collections;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using System.Windows;

namespace WPSPropertyPlugin
{
  /// <summary>
  /// 属性编辑控件
  /// </summary>
  public partial class PropertyPane : UserControl
  {
    private TextEditor txtConfig;

    public PropertyPane()
    {
      InitializeComponent();
      var editor = this.txtConfig = new TextEditor();
      editor.ShowLineNumbers = true;
      editor.Padding = new Thickness(20);
      editor.FontFamily = new System.Windows.Media.FontFamily("Console");
      editor.FontSize = 14;
      editor.TextChanged += Editor_TextChanged;
      editor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("JavaScript");
      this.txtConfigContainer.Child = editor;
    }

    private void Editor_TextChanged(object sender, EventArgs e)
    {
      if (this.SelectedObject != null)
      {
        this.SelectedObject.Config = this.txtConfig.Text;
      }
      this.btnSave.Enabled = true;
    }

    public ShapeProperty SelectedObject
    {
      set
      {
        this.propertyGrid.SelectedObject = value;
        if (value != null)
        {
          this.txtConfig.Text = value.Config;
        }
        else
        {
          this.txtConfig.Text = string.Empty;
        }
      }
      get
      {
        return this.propertyGrid.SelectedObject as ShapeProperty;
      }
    }

    private void btnFormatCode_Click(object sender, EventArgs e)
    {
      if (string.IsNullOrEmpty(this.txtConfig.Text))
      {
        return;
      }
      try
      {
        this.txtConfig.Text = this.txtConfig.Text.FormatJSON();
      }
      catch { }

    }

    private void btnSave_Click(object sender, EventArgs e)
    {
      this.btnSave.Enabled = false;
      if (this.SelectedObject != null)
      {
        this.SelectedObject.SaveConfig();
      }

    }

    private void btnDeleteConfig_Click(object sender, EventArgs e)
    {

    }

    private void btnReset_Click(object sender, EventArgs e)
    {
      WPPPlugin.Reset();
    }

    private void btnCopyConfig_Click(object sender, EventArgs e)
    {
      var shapeName = this.txtCopyShapeName.Text;
      if (string.IsNullOrEmpty(shapeName))
      {
        shapeName = this.SelectedObject.Name.AddNameIndex(-1);
      }
      var config = WPPPlugin.GetShapeConfigByName(shapeName);
      this.txtConfig.Text = config;
    }

    private void btnAddShapeNameIndex_Click(object sender, EventArgs e)
    {
      var shapeProperty = this.SelectedObject;
      if (shapeProperty != null)
      {
        var name = shapeProperty.Name;
        shapeProperty.Name = name.AddNameIndex();
        this.propertyGrid.SelectedObject = shapeProperty;
        this.txtCopyShapeName.Text = name;
      }
    }
  }
}
